var layer;
var form;
var table;
var $;
var tempState;
var element;
layui.use([ 'layer', 'form', 'table', 'jquery' , 'laydate', 'upload', 'tree', 'element'], function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	$ = layui.jquery; 
	tree = layui.tree; 
	element = layui.element;
	reloadOrgChart();
	
	$("body").click(function(){
		$(".father_btns").css('display','none');
	})
	// 添加组织
	$("#add_Organiztion").click(function(){
		$.post($projectPath+'/sys/organization/selectOne',{"oid":oid},function(data){
			var data = data.data;
			if(data.isProject != 1){
				tempState = 0;
				loadMens(0);
				$("#addOrg_form").find(".layui-tab").css("display","block");
				openWindow("addOrg_form", "新增组织", "800px", "400px", false, 0, true, false);
			}else{
				layer.msg("不能再项目下添加组织")
			}
		})
	})
	// 添加项目
	$("#add_project").click(function(){
		$.post($projectPath+'/organization/selectOne',{"oid":oid},function(data){
			var data = data.data;
			if(data.isProject != 1){
				tempState = 0;
				loadMens(0);
				getLocal();
				$("#codeAndPass").css('display','none');
				openWindow("addProject_form", "新增项目", "800px", "500px", false, 0, true, true);
			}else{
				layer.msg("不能再项目下添加项目")
			}
		})
	})
	// 删除项目/组织
	$("#del_Organiztion").click(function(){// 删除一个组织
		if(oid != 1){
			layer.confirm('确定要将'+oname+'删除吗?', {icon: 3, title:'提示',offset:'20%'}, function(index){
	    		$.post($projectPath+'/organization/delete',{"oid":oid},function(data){
	    			if (data.success) {
	    				reloadOrgChart();
					}
	    			layer.msg(data.msg)
	    			layer.close(index);
	    		})
	    	});
		}else{
			layer.msg("顶级组织不能删除")
		}
    	return false;
	})
	// 编辑项目/组织
	$("#edit_rganiztion").click(function(){
		tempState = 1;
		$.post($projectPath+'/organization/selectOne',{"oid":oid},function(data){
			var data = data.data;
			if(oid != lastOid){
				loadMens(oid);
				$("#addOrg_form").find(".layui-tab").css("display","block");
			}else{
				$(".sysPower").empty();
				$(".gzPower").empty();
				$(".zhPower").empty();
				$("#addOrg_form").find(".layui-tab").css("display","none");
			}
			if(data.isProject == 0){
				openWindow("addOrg_form", "编辑组织", "800px", "400px", false, 0, true, false,data);
			}else{
				$("#codeAndPass").css('display','block');
				openWindow("addProject_form", "编辑项目", "800px", "500px", false, 0, true, true,data);
				createMap('', data.lng, data.lat);
			}
		})
	})
	// 提交组织
	form.on('submit(submitOrgForm)', function(data) {
		submitForm(data,'addOrg_form');
		return false;
	})
	// 提交项目
	form.on('submit(submitProjectForm)', function(data) {
		data.field['isProject'] = 1;
		submitForm(data,'addProject_form');
		return false;
	})
	// 生成密钥
	$("#setCodeAndPass").click(function(){
		var f = $("#addProject_form").find("input[name=fid]").val();
		var o = $("#addProject_form").find("input[name=oid]").val();
		$.post($projectPath+'/organization/createCodeAndPass',{"value":f+"-"+o},function(data){
			if(data.success){
				var arr = data.data;
				$("#addProject_form").find("input[name=code]").val(arr[0]);
				$("#addProject_form").find("input[name=pass]").val(arr[1]);
			}
			layer.msg(data.msg,{offset:'2%;'})
		})
	})
	//---------------权限部分-----------
	form.on("checkbox(check0)",function(data){
		menuCheck(data.elem.checked,'addOrg_form','menu0');
	})
	form.on("checkbox(check1)",function(data){
		menuCheck(data.elem.checked,'addOrg_form','menu1');
	})
	form.on("checkbox(check2)",function(data){
		menuCheck(data.elem.checked,'addOrg_form','menu2');
	})
	form.on("checkbox(projectcheck0)",function(data){
		menuCheck(data.elem.checked,'addProject_form','menu0');
	})
	form.on("checkbox(projectcheck1)",function(data){
		menuCheck(data.elem.checked,'addProject_form','menu1');
	})
	form.on("checkbox(projectcheck2)",function(data){
		menuCheck(data.elem.checked,'addProject_form','menu2');
	})
	form.on('checkbox(menu0)', function(data){
		menuSelCheck(data.elem.checked,0,data.value);
	})
	form.on('checkbox(menu1)', function(data){
		menuSelCheck(data.elem.checked,1,data.value);
	})
	form.on('checkbox(menu2)', function(data){
		menuSelCheck(data.elem.checked,1,data.value);
	})
})

/**
 * 选择的菜单2
 * @param check
 * @returns
 */
function menuSelCheck(check,i,id){
	if(check){
		$(".select_"+i+"_"+id).prop("checked",true);
	}else{
		$(".select_"+i+"_"+id).prop("checked",false);
	}
	form.render();
}
/**
 * 选择的菜单
 * @param check
 * @returns
 */
function menuCheck(check,formId,menus){
	if(check){
		$("#"+formId).find("input[name='"+menus+"']").prop("checked",true);
	}else{
		$("#"+formId).find("input[name='"+menus+"']").prop("checked",false);
	}
	form.render();
}
/**
 * 组织/项目提交
 * @returns
 */
function submitForm(data,formId){
	var lay = layer.load(0,{
		shade: [0.2, 'gray']
	});
	if(tempState == 0){
		data.field['fid'] = oid;
	}
	var menu0 = "";
	$("#"+formId).find("input:checkbox[name='menu0']:checked").each(function(i){
		menu0 += $(this).val()+",";
    });
	var menu1 = "";
	$("#"+formId).find("input:checkbox[name='menu1']:checked").each(function(i){
		menu1 += $(this).val()+",";
    });
	var menu2 = "";
	$("#"+formId).find("input:checkbox[name='menu2']:checked").each(function(i){
		menu2 += $(this).val()+",";
    });
	data.field['menu0'] = menu0.substring(0,menu0.length-1);
	data.field['menu1'] = menu1.substring(0,menu1.length-1);
	data.field['menu2'] = menu2.substring(0,menu2.length-1);
	$.ajaxSettings.async = false;
	$.post($projectPath+'/organization/save', data.field, function(res) {
		if(res.success){
			reloadOrgChart();
			layer.closeAll();
			tipsConfirm(res.msg,1);
		}else{
			layer.msg(res.msg);
		}
		layer.close(lay);
	})
	$.ajaxSettings.async = true;
}

var oid,oname;
/**
 *  重新加载组织的视图
 * @returns
 */
function reloadOrgChart(){
	$("#test1").empty();
	$.post($projectPath+'/organization/select', function(res) {
		var oc=$('#test1').orgchart( {
			'data' : res.data[0],
			'depth' : 6,
			'nodeTitle' : 'abbreviation',
			'nodeContent' : 'oname',
			'createNode': function($node, data) {
		        $node[0].id = data.oid
		     }
		});
		oc.$chartContainer.on('mouseenter', '.node', function(e) {
			isColseState=true;
			var $this = $(this);
			var offset = $this.offset();
			oid=$(this).attr("id");
			oname=$this.find('.content').text()
			$(".oname_text").text(oname);
			$(".father_btns").css('display','block');
			$(".father_btns").css("top",offset.top-20).css("left",$(this).width()+offset.left+5).removeClass("gstar_block")
		});
		oc.$chartContainer.on('mouseleave', '.node', function(e) {
			isColseState=false;
		});
	})
}


/**
 * 加载菜单
 * @returns
 */
function loadMens(uoid){
	$.get($projectPath+"/menu/selectTreeAll2",{"urid":0,"uoid":uoid,"rid":0,"oid":lastOid},function(data){
		if(data.success){
			setMenu(data.list);
		}
	})
}
function setMenu(menuList){
	$(".sysPower").empty();
	$(".gzPower").empty();
	$(".zhPower").empty();
	for ( var i = 0; i < menuList.length; i++) {
		var obj = menuList[i];
		var s = "";
		var s1 = "";
		var s2 = "";
		if(obj.isProject == 1){
			s = '<div style="margin-bottom: 5px;height: 20px;">';
			s += ' 		<input type="checkbox" value="'+obj.mid+'" name="menu0" lay-skin="primary" lay-filter="menu0" title="'+obj.mname+'" '+(obj.state > 0?'checked=""':"")+'>';
			s += ' </div>';
			if(obj.child.length>0){//存在子菜单
				s = getChild(obj.child,20,s,obj.mid,0);
			}
		}else if(obj.isProject == 2){
			s1 = '<div style="margin-bottom: 5px;height: 20px;">';
			s1 += ' 		<input type="checkbox" value="'+obj.mid+'" name="menu1" lay-skin="primary" lay-filter="menu1" title="'+obj.mname+'" '+(obj.state > 0?'checked=""':"")+'>';
			s1 += ' </div>';
			if(obj.child.length>0){//存在子菜单
				s1 = getChild(obj.child,20,s1,obj.mid,1);
			}
		}else if(obj.isProject == 3){
			s2 = '<div style="margin-bottom: 5px;height: 20px;">';
			s2 += ' 		<input type="checkbox" value="'+obj.mid+'" name="menu2" lay-skin="primary" lay-filter="menu2" title="'+obj.mname+'" '+(obj.state > 0?'checked=""':"")+'>';
			s2 += ' </div>';
			if(obj.child.length>0){//存在子菜单
				s2 = getChild(obj.child,20,s2,obj.mid,2);
			}
		}
		$(".sysPower").append(s);
		$(".gzPower").append(s1);
		$(".zhPower").append(s2);
	}
	form.render("checkbox");
}
function getChild(childList,left,s,tempId,tempi){
	for(var i = 0; i < childList.length; i++) {
		var obj = childList[i];
		s += '<div style="margin-left: '+left+'px;height: 20px;">';
		s += ' 		<input type="checkbox" value="'+obj.mid+'" class="select_'+tempi+'_'+tempId+'" name="menu'+tempi+'"';
		s += ' lay-skin="primary" lay-filter="menu'+tempi+'" title="'+obj.mname+'" '+(obj.state > 0?'checked=""':"")+'>';
		s += ' </div>';
		if(childList[i].child.length>0){//存在子菜单
			s = getChild(childList[i].child,(left+20),s,obj.mid);
		}
	}
	return s;
}








